import { PERMISSION_ID_ENUM, AuthCodeEnum, AuthEnum, ANDROID_PERMISSIONS_ENUM, IOS_PERMISSIONS_ENUM } from '../enums';
import { queryPermissionNamesByPermissionId } from '../tools';

import { IAuthResponse } from '../types';

import { checkAndroidPermissions } from './android';
import { checkIOSPermissions } from './ios';

export * from './android';
export * from './ios';

/**
 * 检查设备权限
 * @param permissionId 权限ID
 * @param type 1: 读取权限 2: 写入权限；仅Android有效
 * @returns {IAuthResponse}
 * @remark 此方法仅用于基本权限判断，若果需要其他权限检测，安卓请调用checkAndroidPermissions，iOS请调用checkIOSPermissions
 */
export const checkDevicePermission = (permissionId: PERMISSION_ID_ENUM, type = 1): IAuthResponse => {
  let platform;
  // #ifdef H5
  platform = 'ios';
  // #endif
  platform = plus.os.name.toLowerCase();
  const permissionName = queryPermissionNamesByPermissionId({
    permission: permissionId,
    platform,
    type,
  });
  if (!permissionName) {
    return {
      code: AuthCodeEnum.UNDEFINED,
      message: `permission is ${AuthEnum.UNDEFINED}`,
      data: false,
    };
  }
  // ios 逻辑
  if (platform === 'ios') {
    return checkIOSPermissions(permissionName as IOS_PERMISSIONS_ENUM);
  }
  // Android 逻辑
  return checkAndroidPermissions(permissionName as ANDROID_PERMISSIONS_ENUM);
};
